import re
text = 'yeah, but no, but yeah, but no, but yeah'
# startswith:判断字符串以什么开头
print(text.startswith('yeah'))
# endswith:判读字符串以以什么结尾
print(text.endswith("yeah"))
# find()中含子串输出其下标
print(text.find("a"))

text1 = '11/27/2012'
text2 = 'Nov 27, 2012'
if re.match(r'\d+/\d+/\d+', text1):
    print('yes')
else:
    print('no')

# complie()将变量进行预编译,可以多次使用此变量
datepat = re.compile(r'\d+/\d+/\d+')
if datepat.match(text1):
    print('yes')
else:
    print('no')

text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
# findall获取序列中所有满足要求的以列表形式输出
print(datepat.findall(text))

# 分组捕获
datepat = re.compile(r'(\d+)/(\d+)/(\d+)')
m = datepat.match('11/27/2012')
# group()获取对应内容,下标从1开始
print(m.group(0))
print(m.group(1))
# groups()将列表转换为元组
print(m.groups())

# findall()方法搜索全文以列表形式返回
for month, day, year in datepat.findall(text):
    print('{}-{}-{}'.format(year,month,day))

for m in datepat.finditer(text):
    print(m.group())
